package com.example.demo.web;

import com.example.demo.Untils.PachongUtil;
import com.example.demo.entity.UUser;
import com.example.demo.service.IUUserService;
import com.example.demo.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/31/031.
 */
@Controller
public class LoginController {
    private static final Logger logger= LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private LoginService loginService;
    @Autowired
    IUUserService iuUserService;
    @GetMapping("login")
    public String login(){
        return "login";
    }

    @GetMapping("add")
    public String add() {
        return "add";
    }

    @GetMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping(value="ajaxLogin",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> submitLogin(String username, String password) {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        Map<String,Object> resultMap1=new LinkedHashMap<>();
        resultMap1.put("nickname",username);

        List<UUser> uUsers=iuUserService.selectByMap(resultMap1);
        String password1=PachongUtil.MD5( password+uUsers.get(0).getEmail());
        try {

            UsernamePasswordToken token = new UsernamePasswordToken(username, password1);
            SecurityUtils.getSubject().login(token);
            resultMap.put("status", 200);
            resultMap.put("message", "登录成功");
        } catch (Exception e) {
            resultMap.put("status", 500);
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }

    @RequestMapping(value="ajaxRegis",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> submitRegis(String username,String password){

        Map<String,Object> resultMap=loginService.submitRegis(username,password);
        try{
            if(resultMap.containsKey("msg")){
                resultMap.put("success",false);
            }else {
                resultMap.put("success",true);
                resultMap.put("msg","注册成功");
            }

        }catch (Exception e){
            logger.info("注册异常"+e.getMessage());
            e.printStackTrace();
            resultMap.put("success",false);
            resultMap.put("msg","注册异常");
        }
        return  resultMap;

    }

    @RequestMapping(value="logout",method =RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> logout(){
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            //退出
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return resultMap;
    }

}
